package com.payment.ktb.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.payment.ktb.R;
import com.payment.ktb.utils.LogUtil;
import com.squareup.picasso.Picasso;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by admin on 2017/10/9.
 */

public class StartAdvertisementActivity extends BaseActivity {

    @BindView(R.id.iv_startadvertisement_ad)
    ImageView iv_startadvertisement_ad;
    @BindView(R.id.tv_startadvertisement_sec)
    TextView tv_startadvertisement_sec;
    ScheduledExecutorService mSecKilScheduledExecutorService = null;
    int duration = 0;
    private boolean goToMainActivity = true;

    @Override
    protected void onStart() {
        super.onStart();
        mSecKilScheduledExecutorService = Executors.newSingleThreadScheduledExecutor();
        mSecKilScheduledExecutorService.scheduleAtFixedRate(new SecKilTask(), 0, 1, TimeUnit.SECONDS);

    }

    @Override
    protected void onStop() {
        super.onStop();
        if (mSecKilScheduledExecutorService != null) {
            mSecKilScheduledExecutorService.shutdown();
        }

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_startadvertisement);
        ButterKnife.bind(this);
        initData();
    }



    private void initData() {
        Picasso.with(mContext).load(getIntent().getStringExtra("areaPic")).fit().into(iv_startadvertisement_ad);
        duration = getIntent().getIntExtra("duration",5);
        tv_startadvertisement_sec.setText(duration + "");
    }



    @OnClick({R.id.ll_startadvertisement_ignore,R.id.iv_startadvertisement_ad})
    public void clickEvent(View view) {


        switch (view.getId()) {

            //跳过广告
            case R.id.ll_startadvertisement_ignore:

                if (getIntent().getIntExtra("jumpFlg",0) == 1) {
                    startActivity(MainActivity.class);
                    finish();
                }

                break;

            //跳转到广告详情
            case R.id.iv_startadvertisement_ad:
                if (!TextUtils.isEmpty(getIntent().getStringExtra("areaHref"))) {
                    Intent intent = new Intent(mContext,ShowH5WithTitleActivity.class);
                    intent.putExtra("url",getIntent().getStringExtra("areaHref"));
                    intent.putExtra("fromActivity","StartAdvertisementActivity");
                    startActivity(intent);
                    goToMainActivity = false;
                    finish();
                }

                break;

        }

    }


    //用来完成倒计时的任务
    private class SecKilTask implements Runnable{
        public void run() {
            handler.sendEmptyMessage(1);
        }
    }




    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            duration--;
            tv_startadvertisement_sec.setText(duration + "");

            if (duration <=  0 && goToMainActivity) {
                startActivity(MainActivity.class);
                finish();
            }

            }

    };



}
